package com.sllp.C71S3TsllpShop.biz.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sllp.C71S3TsllpShop.bean.Favorite;
import com.sllp.C71S3TsllpShop.bean.FavoriteExample;
import com.sllp.C71S3TsllpShop.bean.FavoriteExample.Criteria;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.FavoriteBiz;
import com.sllp.C71S3TsllpShop.dao.FavoriteMapper;
import com.sllp.C71S3TsllpShop.web.FavoriteAction;

@Service
public class FavoriteBizImpl implements FavoriteBiz {

	@Resource
	private FavoriteMapper fm;
	
	public List<Favorite> findfavorite(User user){
		FavoriteExample fe=new FavoriteExample();
		Criteria c=fe.createCriteria();
		c.andUserEqualTo(user.getId());
		List<Favorite> list=fm.selectByExample(fe);
		return list;
	}
	
	public List<Favorite> findFavoriteWithNull(){
		return fm.selectByExample(null);
	}
	
	//加入收藏
	@SuppressWarnings("null")
	public Result addToMyFav(Favorite favorite) {
		int i ;
		int state = 1;//默认没有被收藏
		FavoriteExample example = new FavoriteExample();
		example.createCriteria().andUserEqualTo(favorite.getUser()).andProductEqualTo(favorite.getProduct());
		List<Favorite> list = fm.selectByExample(example);
		favorite.setState(state);
		System.out.println(favorite.getState());
		if(list!=null && list.size()>0) {
			if(list.get(0).getState()==2) {
				favorite.setState(1);
				i=fm.updateByExampleSelective(favorite, example);
				if(i<0) {
					return new Result(2,"未知错误");
				}else {
					return new Result(1,"取消收藏");
				}
			}else{
				favorite.setState(2);
				i=fm.updateByExampleSelective(favorite, example);
				if(i<0) {
					return new Result(2,"未知错误");
				}else {
					return new Result(1,"收藏");
				}
			}
		}else{
			System.out.println("+++++++=");
			favorite.setState(2);
			fm.insert(favorite);
			return new Result(1,"收藏");
		}
		
		
		
	}
}
